package org.xserver.component.handler.ext.http;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xserver.component.core.XServerHttpContextAttachment;
import org.xserver.component.core.dispatch.http.FileRequestDispatch;
import org.xserver.component.core.dispatch.http.HttpRequestDispatch;
import org.xserver.component.core.dispatch.http.InterfaceRequestDispatch;

public class HttpResponseWriterHandlerFactory {
	private static final Logger log = LoggerFactory
			.getLogger(HttpResponseWriterHandlerFactory.class);

	public static Map<String, HttpResponseWriteHandler> httpResponseWriteHandlerInfos = new HashMap<String, HttpResponseWriteHandler>();

	static {
		for (HttpResponseType httpResponseType : HttpResponseType.values()) {
			try {
				httpResponseWriteHandlerInfos.put(httpResponseType.getType(),
						httpResponseType.getClazz().newInstance());
			} catch (Exception e) {
				log.error("init writeHandlerInfos error", e);
				System.exit(-1);
			}
		}
	}

	/**
	 * Prototype between client and server
	 */
	public enum HttpResponseType {
		FILE("FILE",
				FileHttpResponseWriteHandler.class), INTERFACE("INTERFACE",
				
				InterfaceHttpResponseWriteHandler.class);

		private final String type;
		
		private final Class<? extends HttpResponseWriteHandler> clazz;

		public String getType() {
			return type;
		}

		public Class<? extends HttpResponseWriteHandler> getClazz() {
			return clazz;
		}

		HttpResponseType(
				String type,
				Class<? extends HttpResponseWriteHandler> clazz) {
			this.type = type;
			this.clazz = clazz;
		}
	}

	public static HttpResponseWriteHandler getInstance(String key) {
		return httpResponseWriteHandlerInfos.get(key);
	}
}
